package com.wangweixiong.role.controller;

import com.wangweixiong.role.base.BaseController;
import com.wangweixiong.role.bean.RoleBean;
import com.wangweixiong.role.bean.RoleUserBean;
import com.wangweixiong.role.service.RoleService;
import com.wangweixiong.role.service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name="RoleControllerGetWWX",urlPatterns="/wwx/get")
public class RoleControllerGetWWX extends BaseController {
    private RoleService roleService =new RoleServiceImpl();
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        // 解决中文乱码问题
        servletRequest.setCharacterEncoding("UTF-8");

        HttpServletResponse httpServletResponse=(HttpServletResponse) servletResponse;

        // 解决跨域问题
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");


        String method = servletRequest.getParameter("method");
        // 拿出session种的userid
        HttpServletRequest request= (HttpServletRequest)servletRequest;
        HttpSession session = request.getSession();
        Integer userId = (Integer)session.getAttribute("userId");
        // 根据method的不同进入不同方法
        if(method.equals("select")){
            List<RoleUserBean> roleUserBeans = roleService.findAllRole(userId);
            printData(roleUserBeans!=null,roleUserBeans,500,servletResponse);
        }




    }
}
